//
//  ShapedView.m
//  yuikemall
//
//  Created by Gang.Wang on 12-5-22.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

#import "ShapedView.h"
#import "header.h"


@interface ShapedView ()
{
    CGContextRef mCtx;
}
@end

@implementation ShapedView

@synthesize delegate = delegate_;
@synthesize image;

- (void)dealloc
{
    YRelease(image)
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setBackgroundColor:COLOR_CLEAR];
         mCtx = UIGraphicsGetCurrentContext();
    }
    return self;
}

- (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    BOOL superResult = [super pointInside:point withEvent:event];
    if (!superResult) 
    {
        return superResult;
    }
    
    return [self isVisibleAtPoint:point];
}


- (void) setFrame:(CGRect)frame
{
    [super setFrame:frame];
    [self setNeedsDisplay];
}


- (void) touchesEnded:(NSSet *)touches
            withEvent:(UIEvent *)event
{
//    [super touchesEnded:touches withEvent:event];    
    DELEGATE_CALLBACK(click:, self);
}


- (BOOL) isVisibleAtPoint:(CGPoint) point
{
//    kMustOverrideMethod;
    return YES;
}


- (void) drawRect:(CGRect)rect
{    
    if (!mCtx) {
        mCtx = UIGraphicsGetCurrentContext();  
    }
//    CGContextRestoreGState(mCtx);
    
    UIBezierPath* path = [UIBezierPath bezierPath];   
    [path moveToPoint:CGPointZero];
    [path addLineToPoint:CGPointMake(0, self.height)];
    [path addLineToPoint:CGPointMake(self.width, self.height)];
    [path addLineToPoint:CGPointMake(self.width, 0)];
    [path closePath];    
    //    [path fill];
    //    [path stroke];
    [path addClip];
    CGContextAddPath(mCtx, path.CGPath);
    
    CGContextClip(mCtx);
    //    CGContextEOClip(mCtx);
    
    [self.image drawInRect:self.bounds];
    
//    CGContextRestoreGState(mCtx);  
}


@end
